#include "map.h"
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void Map::populateMap(int number)
{
	int new_num;

	if(number >= Map::total_area/3){
		new_num = total_area/3 - 1;
	}
	else{
		new_num = number;
	}

	int num_left = new_num;

	for(int i=0; i<y_max; ++i){
		for(int j=0; j<x_max; ++j){
			if(num_left > 0){
				int prob = rand() % total_area;

				if(prob < new_num){
					placeBarrier(i,j);
					--num_left;
				}
			}
		}
	}
}

void Map::initializeGrid(){

	for(int i = 0; i < y_max; ++i){
		std::vector<bool> row;
		for(int j=0; j < x_max; ++j){
			row.push_back(false);
		}
		Map::xygrid.push_back(row);
	}
}

void Map::placeBarrier(int y,int x){
	if(y >= y_max){
		return;
	}
	else if(x >= x_max)
	{
		return;
	}

	if(!Map::xygrid[y][x]){
		Map::xygrid[y][x] = true;
	}
}

void Map::resetSize(int y,int x){
	y_max = y;
	x_max = x;
	total_area = y_max*x_max;

	Map::xygrid.clear();

	initializeGrid();
}

bool Map::isBarrierAtLocation(int y,int x){
	return xygrid[y][x];
}
